//
//  IWHomeViewController.m
//
//
//  Created by apple on .
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "TTHomeViewController.h"
#import "TileItem.h"
#import "ShopServiceViewController.h"
#import "PetServiceViewController.h"

#define HEIGHT_TILES_VIEW 240
#define HEIGHT_TOTALMONEY_VIEW (SCREEN_HEIGHT-49-HEIGHT_TILES_VIEW)
#define HEIGHT_TILE_ITEM 120
#define WIDTH_TILE_ITEM (SCREEN_WIDTH/3.0)

#define CASE_ITEM_PETSERVICE 0 //宠物服务
#define CASE_ITEM_SHOPMANAGER 1 //店铺管理
#define CASE_ITEM_GOODMANAGER 2 //商品管理
#define CASE_ITEM_ORDERMANAGER 3 //订单管理
#define CASE_ITEM_CLIENTMANAGER 4 //客户管理
#define CASE_ITEM_SHOPCOUNT 5  //店铺统计

@interface TTHomeViewController ()<TileItemDelegate>

@end

@implementation TTHomeViewController

- (id)init {
    if (self = [super init]) {
        
        self.title = @"店老大";
        self.view.backgroundColor = [UIColor whiteColor];
        
        
        itemsArray = [[NSArray alloc]initWithObjects:@[@"宠物服务",@"店铺管理",@"商品管理"],@[@"订单管理",@"客户管理",@"店铺统计"],nil];
        
        [self initTotalBalanceView];
        [self initContentScroll];
        [self initTilesView];
        }
    return self;
}

-(void)initTotalBalanceView{
    totalBalanceView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,HEIGHT_TOTALMONEY_VIEW+20)];
    totalBalanceView.backgroundColor = RGBCOLOR(230.0, 87.0, 73.0, 1.0);
    [self.view addSubview:totalBalanceView];
    
    //Title
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"店老大";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:24];
    titleLabel.y = 20;
    [titleLabel sizeToFit];
    titleLabel.centerX = totalBalanceView.centerX;
    [totalBalanceView addSubview:titleLabel];
    
    //累计收入 label
    totalIncomeLabel = [[UILabel alloc] init];
    totalIncomeLabel.textColor = [UIColor whiteColor];
    totalIncomeLabel.font = [UIFont boldSystemFontOfSize:24];
    totalIncomeLabel.text = @"累计收入: 0.00";
    totalIncomeLabel.x = 40;
    totalIncomeLabel.y = CGRectGetMaxY(titleLabel.frame) + 60;
    [totalIncomeLabel sizeToFit];
    [totalBalanceView addSubview:totalIncomeLabel];

    
    //本月收入 label
    monthIncomeLabel = [[UILabel alloc] init];
    monthIncomeLabel.textColor = [UIColor whiteColor];
    monthIncomeLabel.font = [UIFont boldSystemFontOfSize:24];
    monthIncomeLabel.text = @"本月收入（元）";
    monthIncomeLabel.x = totalIncomeLabel.x;
    monthIncomeLabel.y = CGRectGetMaxY(totalIncomeLabel.frame) + 20;
    [monthIncomeLabel sizeToFit];
    [totalBalanceView addSubview:monthIncomeLabel];

    //本月收入金额 label
    incomeNumberLabel = [[UILabel alloc] init];
    incomeNumberLabel.textColor = [UIColor whiteColor];
    incomeNumberLabel.font = [UIFont boldSystemFontOfSize:50];
    incomeNumberLabel.text = @"0.00";
    incomeNumberLabel.x = totalIncomeLabel.x;
    incomeNumberLabel.y = CGRectGetMaxY(monthIncomeLabel.frame) + 60;
    [incomeNumberLabel sizeToFit];
    [totalBalanceView addSubview:incomeNumberLabel];
    
    //按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.width = 80;
    btn.height = 50;
    btn.x = CGRectGetWidth(totalBalanceView.frame) - 20 - btn.width;
    btn.y = totalIncomeLabel.y;
    [btn setImage:[UIImage imageNamed:@"rightArrow"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(rightArrowBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [totalBalanceView addSubview:btn];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    self.navigationController.navigationBarHidden = YES;
}

- (BOOL)prefersStatusBarHidden {
    return NO;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
}

-(void)initContentScroll{
    contentScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, HEIGHT_TOTALMONEY_VIEW+20,SCREEN_WIDTH, SCREEN_HEIGHT-HEIGHT_TOTALMONEY_VIEW-64)];
    contentScroll.scrollEnabled = YES;
    contentScroll.backgroundColor = [UIColor clearColor];
    contentScroll.showsHorizontalScrollIndicator = NO;
    contentScroll.showsVerticalScrollIndicator = NO;
    contentScroll.contentSize = CGSizeMake(SCREEN_WIDTH,HEIGHT_TILES_VIEW+1);
    [self.view addSubview:contentScroll];
    
}


-(void)initTilesView{
    tilesView = [[UIView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,HEIGHT_TILES_VIEW)];
    tilesView.backgroundColor = [UIColor whiteColor];
    [contentScroll addSubview:tilesView];
    
    //增加tileItem
    NSInteger tag = 200;
    for (int i = 0;i<[itemsArray count];i++) {
        NSArray * itemArray = [itemsArray objectAtIndex:i];
        for (int j = 0;j<[itemArray count];j++ ) {
            TileItem * tileItem = [[TileItem alloc]initWithFrame:CGRectMake(j*WIDTH_TILE_ITEM, i*HEIGHT_TILE_ITEM, WIDTH_TILE_ITEM, HEIGHT_TILE_ITEM)];
            
            tileItem.iconImg = [UIImage imageNamed:[NSString stringWithFormat:@"tileIcon_%d_%d",i,j]];
            tileItem.titleStr = [itemArray objectAtIndex:j];
            tileItem.tag = tag;
            tileItem.delegate = self;
            
            [tilesView addSubview:tileItem];
            tag++;
        }
    }
    
    for (int i = 0;i < [tilesView.subviews count]; i++) {
        TileItem * tileItem = (TileItem *)[tilesView viewWithTag:200+i];
        //给tileItem加上边框
        switch (i%3) {
            case 2: //最后边一栏，底部增加分割线
            {
                UIView * bottomLine = [[UIView alloc]init];
                bottomLine.frame = CGRectMake(0,HEIGHT_TILE_ITEM-SCREEN_SCALE, WIDTH_TILE_ITEM, SCREEN_SCALE);
                bottomLine.backgroundColor = RGBCOLOR(204.0, 204.0, 204.0, 1.0);
                [tileItem addSubview:bottomLine];
            }
                break;
                
            default://左侧及中间栏，增加底部及右侧分割线
            {
                //底部分割线
                UIView * bottomLine = [[UIView alloc] init];
                bottomLine.frame = CGRectMake(0,HEIGHT_TILE_ITEM-SCREEN_SCALE, WIDTH_TILE_ITEM, SCREEN_SCALE);
                bottomLine.backgroundColor = RGBCOLOR(204.0, 204.0, 204.0, 1.0);
                [tileItem addSubview:bottomLine];
                
                //右侧分割线
                UIView * rightLine = [[UIView alloc]init];
                rightLine.frame = CGRectMake(WIDTH_TILE_ITEM-SCREEN_SCALE,0, SCREEN_SCALE,HEIGHT_TILE_ITEM);
                rightLine.backgroundColor = RGBCOLOR(204.0, 204.0, 204.0, 1.0);
                [tileItem addSubview:rightLine];
            }
                break;
        }
    }
}

#pragma mark - TileItemDelegate
- (void)tileItemAction:(TileItem *)item{//点击其中某一item跳转方法
    NSInteger index = item.tag - 200;
    switch (index) {
            
        case CASE_ITEM_PETSERVICE:{ //宠物服务
            PetServiceViewController *petServiceVc = [[PetServiceViewController alloc] init];
            [self.navigationController pushViewController:petServiceVc animated:YES];
            break;
        }
        case CASE_ITEM_SHOPMANAGER:{    //店铺管理
            break;
        }
        case CASE_ITEM_GOODMANAGER:{    //商品管理
            break;
        }
        case CASE_ITEM_ORDERMANAGER:{   //订单管理
            
            break;
        }
        case CASE_ITEM_CLIENTMANAGER:{  //客户管理
            
            break;
        }
        case CASE_ITEM_SHOPCOUNT:{  //店铺统计
            
            break;
        }
            
        default:
            break;
    }
}

//数据刷新后，调整label的大小
-(void)updateSizeOfLabel:(UILabel *)label{
    CGRect rect = label.frame;
    CGSize size = [label.text sizeWithFont:label.font];
    rect.size.width = size.width;
    label.frame = rect;
}

#pragma mark - UIButton Action 
- (void)rightArrowBtnDidClick:(UIButton *)btn {
    NSLog(@"向右箭头被点击了");
}

@end
